28-Jul-2022

.Outline

  • Recap of the June meeting

  • graphics library

  • 3D warning

  • rgl library

  • plotly library

  • magick library

  • Code Surgery - Sales data analysis

.June meeting

Spatial analysis

.3D warning

Three dimensional objects are very popular but negatively affect the accuracy and speed at which one can interpret a graphic in most cases.

Good for:

  • scatter and surface plots

Bad for:

  • barplots or pie charts.

.graphics library

# simple right circular cone
cone <- function(x, y){
sqrt(x ^ 2 + y ^ 2)
}
 
# prepare variables
x <- y <- seq(-1, 1, length = 30)
z <- outer(x, y, cone)
 
# plot the 3D surface
persp(x, y, z)

.rgl library

library(rgl)
# This is to output a rgl plot in a rmarkdown document.
setupKnitr()
# Add a new column with color to iris
mycolors <- c('royalblue1', 'darkcyan', 'oldlace')
iris$color <- mycolors[ as.numeric(iris$Species) ]
# Plot
plot3d( 
  x = iris$`Sepal.Length`, y = iris$`Sepal.Width`, z = iris$`Petal.Length`, 
  col = iris$color, 
  type = 's', 
  radius = .1,
  xlab="Sepal Length", ylab="Sepal Width", zlab="Petal Length")
# To display in an R Markdown document:
rglwidget()

.plotly library

library(plotly)
# Data: volcano is provided by plotly
# Plot
p <- plot_ly(z = volcano, type = "surface")
p 

.rgl + magick

library(rgl)
library(magick)
mycolors <- c('royalblue1', 'darkcyan', 'oldlace')
iris$color <- mycolors[ as.numeric(iris$Species) ]
# Static chart
plot3d( iris[,1], iris[,2], iris[,3], col = iris$color, type = "s", radius = .2 )
# We can indicate the axis and the rotation velocity
play3d( spin3d( axis = c(0, 0, 1), rpm = 20), duration = 10 )
# Save as a gif
movie3d(
  movie="3dAnimatedScatterplot", 
  spin3d( axis = c(0, 0, 1), rpm = 7),
  duration = 10, 
  dir = "/content/drive/MyDrive/Colab Notebooks/tb-data-science-talks/2022-07-28/presentation/",
  type = "gif", 
  clean = TRUE)

.Example of the Simple Features

.Example of the Surface

## NULL

.Code Surgery - Sales data analysis